package com.thinkingstar.iads.datacenter.entity.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Path.ReturnValueNode;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.ArrayUtil;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.common.utils.StringUtil;
import com.thinkingstar.iads.common.utils.SwapUtil;
import com.thinkingstar.iads.datacenter.entity.DcEquipmentEquip;
import com.thinkingstar.iads.datacenter.entity.DcEquipmentEquipInfo;


/**
 * @ClassName: DaoDcEquipmentEquipInfo 
 * @Description:数据中心_项目_项目设备配备子表维护
 * @author 1157372219@qq.com 
 * @date 2018年3月19日   
 */
@IocBean(args = {"refer:dao"})
public class DaoDcEquipmentEquipInfo extends EntityRepositoryServiceImpl<DcEquipmentEquipInfo>{

	public DaoDcEquipmentEquipInfo(Dao dao) {
		super(dao);
	}

	//根据设备库表id获取 设备配备表信息（带设备类型名称） add 物资总库 设备选型
	public List<Map> getEquipmentEquipInfoByParam(String equip_id){
		List <Map> list = null;
		String sql = "select dect.equipment_children_type_name , deei.* from " +
				"dc_equipment_equip_info deei , dc_equipment_equip dee , dc_equipment de , dc_equipment_children_type dect " +
				"where deei.equipment_equip_id = dee.id and deei.equipment_id = de.id and de.equipment_children_type_id = dect.id " +
				"and deei.del_flag = '0' and dee.del_flag = '0' and de.del_flag = '0' and dect.del_flag = '0' " +
				"and dee.id = '"+equip_id+"'";
		Sql sqls = Sqls.create(sql);
		sqls.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List <Map> list = new ArrayList<>();
				while (rs.next()) {
					Map <String , Object> map = new HashMap<>();
					map.put("id", rs.getString("id"));
					map.put("equipment_id", rs.getString("equipment_id"));
					map.put("equipment_name", rs.getString("equipment_name"));
					map.put("equipment_num", rs.getString("equipment_num"));
					map.put("create_time", rs.getString("create_time"));
					map.put("update_time", rs.getString("update_time"));
					map.put("equipment_children_type_name", rs.getString("equipment_children_type_name"));
					list.add(map);
				}
				SwapUtil.clearNull(list);
				return list;
			}
		});
		dao().execute(sqls);
		list = sqls.getList(Map.class);
		return list;
	}

	//判断是否已经全部初始化过设备配备表
	public List<DcEquipmentEquipInfo> findEquipInfoByEquipId(String equip_id){
		Criteria criteria = Cnd.NEW();
		criteria.where().and("del_flag", "=", false).and("equipment_equip_id", "=", equip_id);
		List <DcEquipmentEquipInfo> data_list = new ArrayList<>();
		data_list = query(criteria);
		return data_list;
	}
	
	//清空设备配备表
	public void clearEquipmentInfoByEquipId(String equip_id){
		List <DcEquipmentEquipInfo> data_list = findEquipInfoByEquipId(equip_id);
		for(DcEquipmentEquipInfo dcEquipmentEquipInfo : data_list){
			delete(dcEquipmentEquipInfo.getId());
		}
	}
	
	public QueryResult findEquipmentEquipInfo(NutMap nutMap){
		List <Map> page_list = null;
		Pager pager = null;
		StringBuffer final_sql = new StringBuffer(" select deei.* , de.equipment_children_type_name  "
				+ " from dc_equipment_equip_info deei "
				+ " left join dc_equipment de on deei.equipment_id = de.id and de.del_flag = '0' "
				+ " where deei.del_flag = '0' ");
		if(nutMap.get("equip_id") != null){
			final_sql.append(" and deei.equipment_equip_id = @equip_id");
		}
		Sql sql = Sqls.create(final_sql.toString());
		if(nutMap.get("equip_id") != null){
			sql.params().set("equip_id", nutMap.get("equip_id"));
		}
		if(nutMap.get("page_num") != null && nutMap.get("page_size") != null){
			pager = new Pager(nutMap.getInt("page_num"),nutMap.getInt("page_size"));
			pager.setRecordCount(findEquipmentEquipNum(nutMap));
			sql.setPager(pager);
		}
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List <Map> list = new ArrayList<>();
				while (rs.next()) {
					Map <String , Object> map = new HashMap<>();
					map.put("id", rs.getString("id"));
					map.put("equipment_id", rs.getString("equipment_id"));
					map.put("equipment_name", rs.getString("equipment_name"));
					map.put("equipment_num", rs.getString("equipment_num"));
					map.put("create_time", rs.getString("create_time"));
					map.put("update_time", rs.getString("update_time"));
					map.put("equipment_children_type_name", rs.getString("equipment_children_type_name"));
					list.add(map);
				}
				SwapUtil.clearNull(list);
				return list;
			}
		});
		dao().execute(sql);
		page_list = sql.getList(Map.class);
		return new QueryResult(page_list, pager);
	}
	
	public int findEquipmentEquipNum(NutMap nutMap){
		StringBuffer final_sql = new StringBuffer(" select count(deei.id) num "
				+ " from dc_equipment_equip_info deei "
				+ " left join dc_equipment de on deei.equipment_id = de.id and de.del_flag = '0' "
				+ " where deei.del_flag = '0' ");
		if(nutMap.get("equip_id") != null){
			final_sql.append(" and deei.equipment_equip_id = @equip_id");
		}
		Sql sql = Sqls.create(final_sql.toString());
		if(nutMap.get("equip_id") != null){
			sql.params().set("equip_id", nutMap.get("equip_id"));
		}
		sql.setCallback(new SqlCallback() {
			int num = 0;
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				while (rs.next()) {
					num = rs.getInt("num");
				}
				return num;
			}
		});
		dao().execute(sql);
		return sql.getInt();
	}
	

}
